package cn.smartorigin.soas.frame.tab;

import java.awt.Component;

import java.awt.Color;
import javax.swing.ImageIcon;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import cn.smartorigin.soas.frame.SoasUtil;
import cn.smartorigin.soas.frame.panel.CellTablePanel;

public class CellTabbedPane extends JTabbedPane {
    private int preferredUnselectedTabWidth = 80;
    private int preferredTabHeight = new ImageIcon("res/images/tab_header_background.png").getIconHeight();
    
    public CellTabbedPane() {
        init();
    }
    
    private void init() {
        this.setFont(SoasUtil.FONT_12_BOLD);
        this.setForeground(SoasUtil.DEFAULT_TEXT_COLOR);
        this.setBorder(null);
        this.setFocusable(false);
        this.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
        this.setOpaque(false);
        this.setUI(new CellTabbedPaneUI(this));
        this.addChangeListener(new ChangeListener() {
        	@Override
            public void stateChanged(ChangeEvent e) {
                //inform each tab component that select changed.
                updateTabComponents();
            }
        });
    }
    
    @Override
    public void addTab(String title, Component component) {
        super.addTab(title, component);
//        int index = this.getTabCount() - 1;
//        CellTabComponent tabComponent = new CellTabComponent(this);
//        tabComponent.setTitle(title);
//        this.setTabComponentAt(index, tabComponent);
//        this.setToolTipTextAt(index, title);
//        updateTabComponents();
    }
    
    public void addTab(String title, Component component, int usrId){
    	this.addTab(title, component);
        int index = this.getTabCount() - 1;
        CellTabComponent tabComponent = new CellTabComponent(this);
        tabComponent.setTitle(title);
        tabComponent.setTabId(usrId);
        this.setTabComponentAt(index, tabComponent);
        this.setToolTipTextAt(index, title);
        updateTabComponents();
    }

    public int getPreferredTabHeight() {
        return preferredTabHeight;
    }
    
    public int getPreferredUnselectedTabWidth() {
        return preferredUnselectedTabWidth;
    }

    private void updateTabComponents() {
        int selectedIndex = this.getSelectedIndex();
        for (int i = 0; i < this.getTabCount(); i++) {
            Component c = this.getTabComponentAt(i);
            if (c instanceof CellTabComponent) {
            	CellTabComponent component = (CellTabComponent) c;
                boolean selected = selectedIndex == i;
                component.updateSelection(selected);
            }
        }
    }

    public boolean getTabId(int usrId){
    	for(int i =0; i<this.getTabCount(); i++){
    		CellTabComponent cp = (CellTabComponent)this.getTabComponentAt(i);
    		if(cp.getCellTabId() == usrId){
    			return true;
    		}
    	}
    	return false;
    }
    
    public void setSelectTab(int usrId){
    	for(int tabIndex =0; tabIndex<this.getTabCount(); tabIndex++){
    		CellTabComponent cp = (CellTabComponent)this.getTabComponentAt(tabIndex);
    		if(cp.getCellTabId() == usrId){
    			this.setSelectedIndex(tabIndex);
    		}
    	}
    }
    
}
